<template>
  <div class="login-container">
    <el-input placeholder="账号" v-model="username"></el-input>
    <el-input placeholder="密码" v-model="password" show-password></el-input>
    <el-checkbox v-model="remember" :label="label" :value="value" @change="check">记住密码</el-checkbox>
    <el-button type="primary" @click="login">登录</el-button>
    <el-button type="primary" @click="register">注册</el-button>
  </div>
</template>

<script>
import service from '@/axios/axiosConfig';
export default {
  data() {
    return {
      username: "",
        password: "",
      remember: false,
    };
  },
    methods: {
    async login() {
      const responseBody = {
        username: this.username,
        password: this.password
      };
        const response = await service.post("http://localhost:8080/common/login", responseBody)
        console.log(response.data.data.permissionId)
            sessionStorage.setItem("username", this.username)
            sessionStorage.setItem("password", this.password)
            sessionStorage.setItem("gender", response.data.data.gender)
            sessionStorage.setItem("location", response.data.data.location)
            sessionStorage.setItem("name", response.data.data.name)
            sessionStorage.setItem("token", response.data.data.token)
            sessionStorage.setItem("permission", response.data.data.permissionId)
            if (response.data.data.permissionId == 1) {
            this.$router.push("/admin")
            }
            else {
            this.$router.push("/index")
        }
        
      },
    register() {
      this.$router.push("/register")
        },
        check() {
          console.log('aaaa')
            
            if (this.remember == 1) {
                localStorage.setItem("username", this.username)
            localStorage.setItem("password", this.password)
            }
        else {
            localStorage.removeItem("username")
        localStorage.removeItem("password")
        }
    }
    },
    created() {
        const username = sessionStorage.getItem("username")
        const password = sessionStorage.getItem("password")
        if(username && password) {
            this.username = username
            this.password = password
        }
        const username1 = localStorage.getItem("username")
        const password1 = localStorage.getItem("password")
        if(username1 && password1) {
            this.username = username1
            this.password = password1
            this.remember = true
        }
  }
}
</script>